/**
 * jquery.wtFormPopulate.js v1.0.0
 * 
 * This plugin will take a json object as data source and
 * will try to find the matching fields in the html form then
 * will populate the fields with values from the data source
 * 
 * @author Yasar Bayar <yasarbayar@gmail.com>
 * @example $('#form_id').formPopulate(<?php echo json_encode($_POST);?>);
 */

(function($) {
    $.fn.wtFormPopulate=function(_post){
        var setValues = function($el, val){
            if(val === null) return;
            switch($el.get(0).nodeName.toLowerCase()){
                case 'input':
                    if ($el.is(':checkbox')) {
                        var isChecked = (val == $el.val() ? 'checked' : '');
                        $el.attr('checked', isChecked).attr('defaultChecked', isChecked);
                    }
                    else {
                        $el.val(val).attr('defaultValue',val);
                    }
                    break;
					
                case 'select':
                    $el.children('option').each(function(){
                        if($(this).val() == val)
                            $(this).attr('selected','selected').attr('defaultSelected','selected');
                    });
                    break;
					
                case 'textarea':
                    $el.text(val).attr('defaultValue',val);
                    break;
					
                case 'span':
                    if ($el.attr('datasource')){
                        eval('var json = '+$el.attr('datasource')+';');
                        if(json) val = json[val];
                    }
                    $el.html(val);
                    break;
                
                case 'img':
                    $el.hide().attr('src', $el.attr('src')+val).fadeIn('slow');
                    break;
            }
        };
		
        var $FORM = $(this);
        $FORM.data('dirty', false);
		
        $.each(_post,function(key,val){
            $el = $FORM.find('#'+key);
            if ($el.size() == 0){
                $el = $FORM.find('[name=' + key + ']');
				
                if ($el.size() == 0) {
                    $el = $FORM.find('[name="' + key + '[]"]');
                }
            }
			
            if($el.size() == 0) return;
			
            if ($el.size()>1 && !$(val).is(':array')) val = eval('['+val+']');
			
            if (val !='' && val instanceof Array){
                switch ($el.get(0).nodeName.toLowerCase()) {
                    case 'input':
                        $(val).each(function(i, item){
                            $FORM.find('[value=' + item + ']').attr('checked', 'checked').attr('defaultChecked','checked');
                        });
                        break;
                    case 'select':
                        $(val).each(function(i, item){
                            $FORM.find('[value=' + item + ']',$el).attr('selected', 'selected').attr('defaultSelected','selected');
                        });
                        break;
                }
                return;
            }
			
			
            if ($.isArray($el)){
                $el.each(function(i, _el){
                    setValues($(_el), val);
                });
            }else{
                setValues($el, val);
            }
        });
    };
})(jQuery);